﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interop.BugTraqProvider;
using System.Runtime.InteropServices;

namespace RedmineIssuesProvider
{
    [ComVisible(true),
        Guid("7E5EC99D-3C3F-4807-9C3B-AAC072401DC0"),
        ClassInterface(ClassInterfaceType.None)]
    public class RedmineProvider : IBugTraqProvider
    {
        #region Implementation of IBugTraqProvider

        public bool ValidateParameters(IntPtr hParentWnd, string parameters)
        {
            return true;
        }

        public string GetLinkText(IntPtr hParentWnd, string parameters)
        {
            return "Redmine";
        }

        public string GetCommitMessage(IntPtr hParentWnd, string parameters, string commonRoot, string[] pathList, string originalMessage)
        {

            if (Properties.Settings.Default.FirstRun && Properties.Settings.Default.FiltersFileName == "")
            {
                System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();

                string dllDir = System.IO.Path.GetDirectoryName(a.Location);

                Properties.Settings.Default.FiltersFileName = dllDir + "\\" + "filters.xml";
                Properties.Settings.Default.DefaultFilter = "Открытые";
                Properties.Settings.Default.FirstRun = false;
                Properties.Settings.Default.Save();
            }
            
            Service service = new Service();

            MainForm fmIssues = new MainForm(service);

            if (fmIssues.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (originalMessage == "")
                    return service.Issues.GetCheckedFixText();
                if (originalMessage.Substring(originalMessage.Length-1) == "\n")
                    return originalMessage + service.Issues.GetCheckedFixText();
                return originalMessage + "\n" + service.Issues.GetCheckedFixText();
            }

            return originalMessage;
            
        }

        #endregion
    }
}
